using System;
using System.Web.UI.WebControls;
using SD.LLBLGen.Pro.ORMSupportClasses;
using VitalShining.WebControls;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMS.AdminTemplate
{
    [Function("Admin.TemplateList")]
    public partial class TemplateList : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageQueryAdapter1.PerformQuery();

                controlCondition.InitalControls("Admin.TemplateList", "Template");
            }
        } 

        protected void btnAddTemplate_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AdminTemplate/TemplateDetail.aspx");
        }
        protected void btnSearchTemplate_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }

        protected void btnViewAll_Click(object sender,EventArgs e)
        {
            controlCondition.InitalControls("Admin.TemplateList", "Template");
            PageQueryAdapter1.PerformQuery();
        }


        protected void gridTemplate_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (TemplateEntity)e.Row.DataItem; 
                e.Row.Attributes.Add("TemplateId", entity.TemplateId.ToString()); 
            }
        }

        protected void gridTemplate_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DeleteItem":
                    //if (TemplateBusiness.DeleteTemplate(new Guid(e.CommandArgument.ToString())))
                    //{
                    //    PageQueryAdapter1.PerformQuery();
                    //    base.Alert("Successfully deleted!");
                    //}
                    break;
                case "ViewItem":
                    Response.Redirect("~/AdminTemplate/TemplateDetail.aspx?TemplateID=" + e.CommandArgument);
                    break;
             
            }
        }


      


        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            SortExpression sort;
            switch (gridTemplate.SortExpression)
            {
                case "TemplateName":
                    sort = new SortExpression(TemplateFields.TemplateName | (SortOperator)(int)gridTemplate.SortDirection);
                    break;
                case "TemplateSubject":
                    sort = new SortExpression(TemplateFields.TemplateSubject | (SortOperator)(int)gridTemplate.SortDirection);
                    break;
                case "TemplateID":
                    sort = new SortExpression(TemplateFields.TemplateId | (SortOperator)(int)gridTemplate.SortDirection);
                    break;
                default:
                    sort = new SortExpression(TemplateFields.CreateDate | SortOperator.Descending);
                    break;
            }

            var collection = new TemplateCollection();
            collection.GetMulti((PredicateExpression) e.Condition,0, sort,null, e.PageIndex + 1, e.PageSize);
            e.DataSource = collection;
        }


        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            e.RecordCount = GetTemplateList((PredicateExpression)e.Condition, null, 0, 0).Count;
        }
        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            var condition = controlCondition.GetPredicateExpression(); 
            e.Condition = condition;
        }



        private TemplateCollection GetTemplateList(PredicateExpression condition,SortExpression sort,int pageSize,int pageIndex)
        {
            var collection = new TemplateCollection();
            collection.GetMulti(condition, 0, sort, null, pageIndex, pageSize);
            return collection;
        }

        protected void btnCreateNew_Click(object sender,EventArgs e)
        {
            Response.Redirect("~/AdminTemplate/TemplateDetail.aspx");
        }
    }
}
